﻿package com.example.news;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;



class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{

    private NewsData newsData;
    private Context context;
    public MyAdapter(Context context,NewsData newsData){
        this.newsData=newsData;
        this.context=context;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.news_list_item,parent,false);
        final MyViewHolder holder=new MyViewHolder(view);
        holder.dataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=holder.getAdapterPosition();
                Intent intent=new Intent(context,WebActivity.class);
                String url=newsData.getResult().getData().get(position).getUrl();
                intent.putExtra("URI",url);
                context.startActivity(intent);

            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Log.e("MainActivity","newData===="+newsData);
        NewsData.ResultBean.DataBean dataBean=newsData.getResult().getData().get(position);
        holder.tvTitle.setText(dataBean.getTitle());
        holder.tvAuthor.setText(dataBean.getAuthor_name());
        holder.tvTime.setText(dataBean.getDate());
        Glide.with(context).load(Uri.parse(dataBean.getThumbnail_pic_s())).into(holder.newsHeadImg);

    }

    @Override
    public int getItemCount() {
        return newsData.getResult().getData().size();
    }

}
